package com.li.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.li.common.CustomException;
import com.li.entity.Category;
import com.li.entity.Dish;
import com.li.entity.Employee;
import com.li.entity.Setmeal;
import com.li.mapper.CategoryMapper;
import com.li.mapper.EmployeeMapper;
import com.li.service.CategoryService;
import com.li.service.DishService;
import com.li.service.EmployeeService;
import com.li.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;

    /**
     * 根据Id删除分类，删除之前需要进行判断
     *
     * @param id
     */
    @Override
    public void remove(Long id) {

        LambdaQueryWrapper<Dish> dishWrapper = new LambdaQueryWrapper();
        //添加查询条件，根据分类id进行查询
        dishWrapper.eq(Dish::getCategoryId, id);
        int count1 = dishService.count(dishWrapper);
        //查询当前分类是否关联菜品
        if (count1 > 0) {
            //已经关联菜品，抛出异常
            throw new CustomException("关联菜品，不能删除");

        }
        //查询当前分类是否关联套餐
        LambdaQueryWrapper<Setmeal> setmealWrapper = new LambdaQueryWrapper();
        //添加查询条件，根据分类id进行查询
        setmealWrapper.eq(Setmeal::getCategoryId, id);
        int count2 = setmealService.count(setmealWrapper);
        if (count2 > 0) {
            //已经关联套餐，抛出异常
            throw new CustomException("关联套餐，不能删除");
        }
        //正常删除
        super.removeById(id);
    }
}
